import { isNullOrUndefined } from "util";

interface IZzTreeNode {
    id: string,
    name: string;
    parentId: string;
    children: IZzTreeNode[];
}
export class ZzTreeNode implements IZzTreeNode{
    public id: string;
    public key: any;
    public name: string;
    public parentId: string;
    public children: ZzTreeNode[] = [];
    public isExpanded = false;
    public isLeaf = false;
    public isSelected = false;
    [key: string]: any;   // constructor之后可添加的参数
    public constructor(param?) {
        (!isNullOrUndefined(param.id)) && (this.id = param.id);
        (!isNullOrUndefined(param.key)) && (this.key = param.key);
        (!isNullOrUndefined(param.name)) && (this.name = param.name);
        (!isNullOrUndefined(param.parentId)) && (this.parentId = param.parentId);
        (!isNullOrUndefined(param.children)) && (this.children = param.children);
        (!isNullOrUndefined(param.isExpanded)) && (this.isExpanded = param.isExpanded);
        (!isNullOrUndefined(param.isSelected)) && (this.isSelected = param.isSelected);
    }
}
interface IParam {
    checkedId: string, topParentId: string
}
export function createTreeNodeFromArray(result: IZzTreeNode[],arr: IZzTreeNode[], param:IParam, callback?: (parentObj: IZzTreeNode,childObj: IZzTreeNode)=>void) {
    for(let item of arr) {
        if(item.parentId == param.topParentId) {
            let temp = new ZzTreeNode(item);
            result.push(temp);
            createTreeNodeFromArray(temp.children, arr, {checkedId: param.checkedId, topParentId: item.id}, callback);
        }
    }
    // for(let i = 0; i < arr.length; i++)
    return result;
}


